import GameManager from "../Managers/GameManager";
import SoundManager from "../Managers/SoundManager";
import AndroidTool, { ADEnum } from "../Tools/AndroidTool";
import Banner from "../Tools/Banner";
import MainUI from "./MainUI";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameUI extends cc.Component {
    private static instance:GameUI;
    static get Instance(){
        return this.instance;
    }
    
    @property(cc.Node)
    right:cc.Node;
    @property(cc.Node)
    wrong:cc.Node;
    @property(cc.Node)
    victoryPanel:cc.Node;
    @property(cc.Node)
    failPanel:cc.Node;
    @property(cc.Node)
    answerPanel:cc.Node;
    @property(cc.Sprite)
    answerDraw:cc.Sprite;
    @property([cc.SpriteFrame])
    answerSpf:cc.SpriteFrame[] = [];

    VTimeout:number;
    FTimeout:number;
    isFinish = false;
    level = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        GameUI.instance = this;
    }

    start () {
        this.LevelInit();
        Banner.Instance.showBanner();
    }

    // update (dt) {}

    LevelInit(){
        this.level = MainUI.Instance.levelNum;
    }

    Victory(){
        if(this.isFinish) return;
        console.log("victory");
        AndroidTool.ShowAD(ADEnum.ChaPing);
        Banner.Instance.CreateCustomAd();
        SoundManager.Instance.PlayWin();
        this.isFinish = true;
        this.right.active = true;
        var unlock = parseInt(localStorage.getItem("LevelUnlock"));
        if(MainUI.Instance.levelNum == unlock) localStorage.setItem("LevelUnlock", (MainUI.Instance.levelNum + 1).toString());
        this.VTimeout = setTimeout(() => {
            this.victoryPanel.active = true;
        }, 1500);
    }

    Fail(){
        if(this.isFinish) return;
        console.log("fail");
        AndroidTool.ShowAD(ADEnum.ChaPing);
        Banner.Instance.CreateCustomAd();
        SoundManager.Instance.PlayFail();
        this.isFinish = true;
        this.wrong.active = true;
        this.FTimeout = setTimeout(() => {
            this.failPanel.active = true;
        }, 1500);
    }

    Back(){
        if(this.VTimeout) clearTimeout(this.VTimeout);
        if(this.FTimeout) clearTimeout(this.FTimeout);
        cc.director.loadScene("MainScene");
    }

    Reset(){
        if(this.VTimeout) clearTimeout(this.VTimeout);
        if(this.FTimeout) clearTimeout(this.FTimeout);
        cc.director.loadScene("GameScene");
        Banner.Instance.CreateCustomAd();
        AndroidTool.ShowAD(ADEnum.ChaPing);
    }

    Answer(){
        if(this.isFinish) return;
        SoundManager.Instance.PlayButton();
        // this.AnswerReward();
        // Banner.Instance.CreateVideo(this.AnswerReward);
        AndroidTool.ShowAD(ADEnum.GameVideo2);
    }

    AnswerReward(){
        GameUI.Instance.answerPanel.active = true;
    }

    AnswerClose(){
        SoundManager.Instance.PlayButton();
        this.answerPanel.active = false;
    }

    Next(){
        SoundManager.Instance.PlayButton();
        // this.NextReward();
        // Banner.Instance.CreateVideo(this.NextReward);
        AndroidTool.ShowAD(ADEnum.GameVideo3);
    }

    NextReward(){
        MainUI.Instance.levelNum++;
        var unlock = parseInt(localStorage.getItem("LevelUnlock"));
        if(MainUI.Instance.levelNum > unlock) localStorage.setItem("LevelUnlock", MainUI.Instance.levelNum.toString()); 
        if(GameUI.Instance.VTimeout) clearTimeout(this.VTimeout);
        if(GameUI.Instance.FTimeout) clearTimeout(this.FTimeout);
        if(MainUI.Instance.levelNum < 14) {
            cc.director.loadScene("GameScene");
            AndroidTool.ShowAD(ADEnum.ChaPing);
            Banner.Instance.CreateCustomAd();
        }
        else cc.director.loadScene("MainScene");
    }
}
